package ClassWork.Work_02;

import java.util.Objects;
import java.util.Scanner;

public class UserManagementSystem {
    public static String[][] arr = new String[20][5];
    public static void searchName(){
        Scanner sc = new Scanner(System.in);
        System.out.println("输入要查询用户的姓名");
        String name = sc.nextLine();
        for (int i = 0; i < arr.length; i++) {
            if (arr[i][1].equals(name)) {
                System.out.println(arr[i][0] + " " + arr[i][1] + " " + arr[i][2] + " " + arr[i][3] + " " + arr[i][4]);
                System.out.println("=========================");
            }
        }
    }
    public static void searchID(){
        Scanner sc = new Scanner(System.in);
        System.out.println("输入要查询用户的ID");
        String id = sc.nextLine();
        for (int i = 0; i < arr.length; i++) {
            if(arr[i][0].equals(id)){
                System.out.println(arr[i][0] + " " + arr[i][1] + " " + arr[i][2] + " " + arr[i][3] + " " + arr[i][4]);
                System.out.println("=========================");
            }
        }
    }
    public static void searchAll(){
        for(int i = 0; i < arr.length; i++){
            if(arr[i][0] == null){
                continue;
            }
            System.out.println(arr[i][0] + " " + arr[i][1] + " " + arr[i][2] + " " + arr[i][3] + " " + arr[i][4]);
            System.out.println("=========================");
        }
    }
    public static void searchUser(){
        Scanner sc = new Scanner(System.in);
        System.out.println("查询全部的用户---------1");
        System.out.println("根据ID查询用户---------2");
        System.out.println("根据姓名查询全用户------3");
        System.out.println("请输入要做的操作");
        int choice = sc.nextInt();
        switch(choice){
            case 1:
                searchAll();
                break;
            case 2:
                searchID();
                break;
            case 3:
                searchName();
                break;
        }
    }
    public static void searchUser(int index){
        System.out.println("您的信息是：");
        System.out.println(arr[index][0] + " " + arr[index][1] + " " + arr[index][2] + " " + arr[index][3] + " " + arr[index][4]);
        System.out.println("=========================");
    }
    public static void updateUser(){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要修改用户的ID号码");
        int index = sc.nextInt();
        for (int i = 0; i < arr.length; i++) {
            if(index == i){
                System.out.println(arr[i][0] + " " + arr[i][1] + " " + arr[i][2] + " " + arr[i][3] + " " + arr[i][4]);
                System.out.println("=========================");
                break;
            }
        }
        System.out.println("请输入要更改的姓名");
        arr[index][1] = sc.next();
        System.out.println("请输入要更改的密码");
        arr[index][2] = sc.next();
        System.out.println("请输入要更改的邮箱");
        arr[index][3] = sc.next();
        System.out.println("请输入要更改的权限");
        arr[index][4] = sc.next();
        System.out.println("更改成功");
    }
    public static void updateUser(int index){
        Scanner sc = new Scanner(System.in);
        System.out.println("您现在的信息是：");
        System.out.println(arr[index][0] + " " + arr[index][1] + " " + arr[index][2] + " " + arr[index][3] + " " + arr[index][4]);
        System.out.println("=========================");
        System.out.println("请输入要修改的姓名");
        arr[index][1] = sc.next();
        System.out.println("请输入要修改的密码");
        arr[index][2] = sc.next();
        System.out.println("请输入要修改的邮箱");
        arr[index][3] = sc.next();
        System.out.println("修改成功");
    }
    public static void deleteUser(int index){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要删除用户的ID号码");
        String id = sc.next();
        if(id.equals(arr[index][0])){
            System.out.println("不能删除当前的用户");
            return;
        }
        for(int i = 0; i < arr.length; i++){
            if(arr[i][0].equals(id)){
                arr[i][0] = null;
                arr[i][1] = null;
                arr[i][2] = null;
                arr[i][3] = null;
                arr[i][4] = null;
                break;
            }
        }
        System.out.println("删除用户成功");
    }
    public static void addUser(){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要增添的用户名");
        String name = sc.next();
        System.out.println("请输入要增添的用户密码");
        String password = sc.next();
        System.out.println("请输入要增添的用户邮箱");
        String email = sc.next();
        System.out.println("请输入要增添的用户权限");
        String power = sc.next();
        for(int i = 0; i < arr.length; i++){
            if(Objects.equals(arr[i][1], name)){
                System.out.println("该用户已存在！");
                break;
            }else if(arr[i][1] == null){
                arr[i][0] = String.valueOf(i);
                arr[i][1] = name;
                arr[i][2] = password;
                arr[i][3] = email;
                arr[i][4] = power;
                System.out.println("添加成功。。。");
                break;
            }
        }
    }
    public static void userpage(int index){
        Scanner sc = new Scanner(System.in);
        System.out.println("欢迎登录主窗体");
        System.out.println(arr[index][1] +"您好：" + " " + "您的权限是：" + arr[index][4]);
        System.out.println("=========================");
        while(true){
            System.out.println("修改自己的信息-------------1");
            System.out.println("查询自己的信息-------------2");
            System.out.println("程序退出------------------3");
            int choice = sc.nextInt();
            switch (choice) {
                case 1:
                    updateUser(index);
                    break;
                case 2:
                    searchUser(index);
                    break;
                case 3:
                    //System.exit(0);
                    return;
                default:
                    System.out.println("请重新输入！");
            }
        }
    }
    public static void adminpage(int index){
        Scanner sc = new Scanner(System.in);
        System.out.println("欢迎登录主窗体");
        System.out.println(arr[index][1] +"您好：" + " " + "您的权限是：" + arr[index][4]);
        System.out.println("=========================");
        while(true){
            System.out.println("添加用户------------------1");
            System.out.println("删除用户------------------2");
            System.out.println("修改用户------------------3");
            System.out.println("查询用户------------------4");
            System.out.println("程序退出------------------5");
            int choice = sc.nextInt();
            switch (choice) {
                case 1:
                    addUser();
                    break;
                case 2:
                    deleteUser(index);
                    break;
                case 3:
                    updateUser();
                    break;
                case 4:
                    searchUser();
                    break;
                case 5:
                    //System.exit(0);
                    return;
                default:
                    System.out.println("请重新输入！");
            }
        }
    }
    public static void registration(){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入账号：");
        String user = sc.next();
        System.out.println("请输入密码：");
        String pass = sc.next();
        System.out.println("请输入邮箱：");
        String mail = sc.next();
        for(int i = 0; i < arr.length; i++){
            if(Objects.equals(arr[i][1], user)){
                System.out.println("该账号已注册！");
                System.out.println("=========================");
                break;
            }else if(arr[i][1] == null){
                arr[i][0] = String.valueOf(i);
                arr[i][1] = user;
                arr[i][2] = pass;
                arr[i][3] = mail;
                arr[i][4] = "普通用户";
                System.out.println("注册成功。。。");
                System.out.println("=========================");
                break;
            }
        }
    }
    public static int login(){
        int index = -1;
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入账号：");
        String username = sc.nextLine();
        System.out.println("请输入密码：");
        String password = sc.nextLine();
        for(int i = 0; i < arr.length; i++){
            if(Objects.equals(arr[i][1], username) && Objects.equals(arr[i][2], password)){
                index = i;
                break;
            }
        }
        return index;
    }
    public static void main(String[] args) {
        System.out.println("用户管理系统");
        System.out.println("=========================");
        Scanner sc = new Scanner(System.in);
        arr[0][0] = "0";
        arr[0][1] = "admin";
        arr[0][2] = "123456";
        arr[0][3] = "admin@test.com";
        arr[0][4] = "管理员";
        while(true){
            System.out.println("用户登录------------------1");
            System.out.println("用户注册------------------2");
            System.out.println("退出程序------------------3");
            int choice = sc.nextInt();
            switch (choice) {
                case 1:
                    System.out.println("用户登录界面");
                    System.out.println("=========================");
                    int index = login();
                    if(index == -1){
                        System.out.println("登陆失败。。。");
                        System.out.println("=========================");
                    }else{
                        System.out.println("登录成功。。。");
                        System.out.println("=========================");
                        if(Objects.equals(arr[index][4],"管理员")){
                            adminpage(index);
                        }else{
                            userpage(index);
                        }
                    }
                    break;
                case 2:
                    System.out.println("用户注册界面");
                    System.out.println("=========================");
                    registration();
                    break;
                case 3:
                    System.exit(0);
                default:
                    System.out.println("请重新输入！");
            }
        }
    }
}
